<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选分类 -->
			<cl-filter label="分类">
				<cl-select prop="category" :options="options.category" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索名称、品牌" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="goodsList-list" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, reactive, ref } from "vue";
import { useDict } from "/$/dict";
const { dict } = useDict();
const { service } = useCool();

// 选项
const options = reactive({
	category: [
		{ label: "普通商品", value: 0 },
		{ label: "热销商品", value: 1 },
		{ label: "新品", value: 2 }
	],
	statusType: [
		{ label: "下架", value: 0 },
		{ label: "上架", value: 1 },
		{ label: "已售", value: 2 }
	]
});
let anchorOptions = ref<any>([]);
// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "名称",
			prop: "name",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "主播",
			prop: "anchorId",
			component: {
				name: "el-select",
				options: anchorOptions,
				props: { clearable: true }
			},
			col: {
				span: 24
			},
			required: true
		},
		{
			label: "价格",
			prop: "price",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "库存",
			prop: "stock",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "分类",
			prop: "category",
			component: { name: "el-radio-group", options: dict.get("goodsType") },
			value: 0,
			required: true
		},
		{
			label: "品牌",
			prop: "brand",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "封面图",
			prop: "coverUrl",
			component: { name: "cl-upload" },
			required: true
		},
		{
			label: "图片",
			prop: "images",
			component: { name: "cl-upload", props: { multiple: true } },
			required: true
		},
		{
			label: "销量",
			prop: "sales",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "状态",
			prop: "status",
			component: { name: "el-radio-group", options: options.statusType },
			value: 1,
			required: true
		},
		{
			label: "描述",
			prop: "description",
			component: { name: "cl-editor-wang", props: { type: "textarea", rows: 4 } }
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "名称", prop: "name", minWidth: 140 },
		{ label: "价格", prop: "price", minWidth: 140 },
		{ label: "库存", prop: "stock", minWidth: 140 },
		{ label: "分类", prop: "category", dict: dict.get("goodsType"), minWidth: 120 },
		{ label: "品牌", prop: "brand", minWidth: 140 },
		// { label: "描述", prop: "description", showOverflowTooltip: true, minWidth: 200 },
		{
			label: "图片",
			prop: "coverUrl",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{ label: "销量", prop: "sales", minWidth: 140 },
		{ label: "状态", prop: "status", minWidth: 100, dict: options.statusType },
		{ label: "标签", prop: "tags", dict: [], minWidth: 120 },
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.goodsList.list
	},
	(app) => {
		app.refresh();
	}
);

const GetAnchorOptions = async () => {
	let data = await service.demo.info.list();
	anchorOptions.value = data.map((o: any) => {
		return {
			value: o.id,
			label: o.name
		};
	});
};

onMounted(() => {
	GetAnchorOptions();
});

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
